package com.ruoyi.system.service.impl;

import java.text.Collator;
import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TWorkOrderMapper;
import com.ruoyi.system.domain.TWorkOrder;
import com.ruoyi.system.service.ITWorkOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工单执行列Service业务层处理
 * 
 * @author wuym
 * @date 2021-04-24
 */
@Service
public class TWorkOrderServiceImpl implements ITWorkOrderService 
{
    @Autowired
    private TWorkOrderMapper tWorkOrderMapper;

    /**
     * 查询工单执行列
     * 
     * @param id 工单执行列ID
     * @return 工单执行列
     */
    @Override
    public TWorkOrder selectTWorkOrderById(Long id)
    {
        return tWorkOrderMapper.selectTWorkOrderById(id);
    }
    /**
     * 查询工单执行列
     *
     * @param orderNo 工单编号
     * @return 工单执行列
     */
    @Override
    public List<TWorkOrder> selectTWorkOrderByOrderNo(String orderNo)
    {
        return tWorkOrderMapper.selectTWorkOrderByOrderNo(orderNo);
    }

    @Override
    public TWorkOrder selectTWorkOrderByMesNo(String mesNo)
    {
        return tWorkOrderMapper.selectTWorkOrderByMesNo(mesNo);
    }
    /**
     * 查询工单执行列列表
     * 
     * @param tWorkOrder 工单执行列
     * @return 工单执行列
     */
    @Override
    public List<TWorkOrder> selectTWorkOrderList(TWorkOrder tWorkOrder)
    {
        return tWorkOrderMapper.selectTWorkOrderList(tWorkOrder);
    }

    /**
     * 新增工单执行列
     * 
     * @param tWorkOrder 工单执行列
     * @return 结果
     */
    @Override
    public int insertTWorkOrder(TWorkOrder tWorkOrder)
    {
        tWorkOrder.setCreateTime(DateUtils.getNowDate());
        return tWorkOrderMapper.insertTWorkOrder(tWorkOrder);
    }

    /**
     * 修改工单执行列
     * 
     * @param tWorkOrder 工单执行列
     * @return 结果
     */
    @Override
    public int updateTWorkOrder(TWorkOrder tWorkOrder)
    {
        tWorkOrder.setUpdateTime(DateUtils.getNowDate());
        return tWorkOrderMapper.updateTWorkOrder(tWorkOrder);
    }

    /**
     * 删除工单执行列对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTWorkOrderByIds(String ids)
    {
        return tWorkOrderMapper.deleteTWorkOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工单执行列信息
     * 
     * @param id 工单执行列ID
     * @return 结果
     */
    @Override
    public int deleteTWorkOrderById(Long id)
    {
        return tWorkOrderMapper.deleteTWorkOrderById(id);
    }

    /**
     * 按工单状态统计
     * @return
     */
    @Override
    public Map<String, Integer> countByWorkStatus(){
        List<TWorkOrder> orderList = tWorkOrderMapper.countByWorkStatus();
        Map<String, Integer>map = new HashMap<>();
        if(orderList != null && !orderList.isEmpty()){
            for (TWorkOrder order : orderList){
                map.put(order.getWorkStatus(), order.getCount());
            }
        }
        return map;
    }
    /**
     * 获取班组列表
     *
     * @return 获取班组列表
     */
    @Override
    public List<String> getWorkGroupNames()
    {
        List<String>result = new ArrayList<>();
        List<String> data = tWorkOrderMapper.getWorkGroupNames();
        for (String tmp : data){
            if(StringUtils.isNotBlank(tmp)){
                result.add(tmp);
            }
        }
        return result;
    }
}
